package com.longshihan.carservice.controller;

import com.github.pagehelper.PageInfo;
import com.google.gson.reflect.TypeToken;
import com.longshihan.carservice.http.APIResponse;
import com.longshihan.carservice.model.OrderDomain;
import com.longshihan.carservice.model.OrderGoodsDomain;
import com.longshihan.carservice.service.Cart.CartService;
import com.longshihan.carservice.service.order.OrderService;
import com.longshihan.carservice.utils.GsonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Type;
import java.util.List;

/**
 * @author longhe
 * @time
 */
@Api("订单相关")
@Controller
@RequestMapping("/order")
public class OrderController extends BaseController {


    @Autowired
    private OrderService orderService;


    @ApiOperation(value = "创建订单")
    @PostMapping(value = "/createOrder")
    @ResponseBody
    public APIResponse createOrder(HttpServletRequest request, HttpServletResponse response,
                               @ApiParam(name = "userId", value = "用户ID", required = true) @RequestParam(name = "userId", required = true) String userId,
                               @ApiParam(name = "goods", value = "数据", required = true) @RequestParam(name = "goods", required = true) String goods,

                                   @ApiParam(name = "note", value = "订单备注") @RequestParam(name = "note") String note,
                                   @ApiParam(name = "address", value = "用户地址") @RequestParam(name = "address") String address,
                                   @ApiParam(name = "deliverTime", value = "生效时间") @RequestParam(name = "deliverTime") String deliverTime) {
        try {
            OrderDomain orderDomain=new OrderDomain();
            orderDomain.setGoods(goods);
            orderDomain.setAddress(address);
            orderDomain.setDeliverTime(deliverTime);
            orderDomain.setNote(note);
            orderDomain.setUserId(userId);
            Type type = new TypeToken<List<OrderGoodsDomain>>() {}.getType();
            List<OrderGoodsDomain> cartBeans = (List<OrderGoodsDomain>) GsonUtils.JSONToObject(goods, type);
            orderDomain.setGoodsBean(cartBeans);
            orderService.createOrder(userId, orderDomain);
            return APIResponse.success();
        } catch (Exception e) {
            String msg = "生成订单失败失败";
            return APIResponse.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "获取用户指定订单信息")
    @PostMapping(value = "/getOrderbyId")
    @ResponseBody
    public APIResponse getOrderbyId(HttpServletRequest request, HttpServletResponse response,
                                    @ApiParam(name = "orderId", value = "订单ID", required = true) @RequestParam(name = "orderId", required = true) String orderId
    ) {
        try {

            OrderDomain orderDomain=orderService.getOrderGoods(orderId);
            if (orderDomain != null) {
                return APIResponse.success(GsonUtils.toJsonString(orderDomain));
            } else {
                return APIResponse.fail("获取不到订单信息");
            }

        } catch (Exception e) {
            String msg = "获取不到订单信息";
            return APIResponse.fail(e.getMessage());
        }
    }


    @ApiOperation(value = "获取用户所有的订单")
    @PostMapping(value = "/getUserOrders")
    @ResponseBody
    public APIResponse getUserOrders(HttpServletRequest request, HttpServletResponse response,
                                    @ApiParam(name = "userId", value = "用户ID", required = true) @RequestParam(name = "userId", required = true) String userId,
                                    @ApiParam(name = "pageNum", value = "页面数", defaultValue = "1") @RequestParam(name = "pageNum", defaultValue = "1") int pageNum,
                                    @ApiParam(name = "pagesize", value = "单次查询的个数", required = true, defaultValue = "20") @RequestParam(name = "pagesize", required = true, defaultValue = "20") int pagesize
    ) {
        try {
            if (pageNum == 0) {
                pageNum = 1;
            }
            if (pagesize == 0) {
                pagesize = 20;
            }
            PageInfo<OrderDomain> orderDomainPageInfo=orderService.getUserOrder(userId,pageNum,pagesize);
            if (orderDomainPageInfo != null) {
                return APIResponse.success(GsonUtils.toJsonString(orderDomainPageInfo));
            } else {
                return APIResponse.fail("获取不到订单列表信息");
            }

        } catch (Exception e) {
            String msg = "获取不到订单列表信息";
            return APIResponse.fail(e.getMessage());
        }
    }
}
